﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using SerializeEx;

public class MySerializeListener : CustomSerializeListener
{
	public bool isCustomSerializable(System.Type t)
	{
		if(t == typeof(SpriteEx))
		{
			return true;
		}

		return false;
	}

    public void Write(System.IO.BinaryWriter binaryWriter, object obj, Dictionary<Object, short> objIndexMap, List<Object> objList, HashSet<System.Type> typeSet)
    {
    	if(obj is SpriteEx)
    	{
    		SpriteEx sprite = obj as SpriteEx;
    		binaryWriter.Write((byte)1);
			SerializableFieldContainerHelper.WriteValueToBytes(binaryWriter, sprite.sprite, SerializableFieldContainerHelper.UnityObjectType, objIndexMap, objList, typeSet);
			SerializableFieldContainerHelper.WriteValueToBytes(binaryWriter, sprite.sheetInfo, SerializableFieldContainerHelper.UnityObjectType, objIndexMap, objList, typeSet);
			sprite.spriteInfo.Write(binaryWriter);
    	}
    }

    public object Read(System.IO.BinaryReader binaryReader, Object[] objs)
    {
    	byte t = binaryReader.ReadByte();
    	if(t == 1)
    	{
    		SpriteEx sprite = new SpriteEx();
			sprite.sprite = SerializableFieldContainerHelper.ReadValueFromBytes(binaryReader, SerializableFieldContainerHelper.UnityObjectType, objs) as Sprite;
			sprite.sheetInfo = SerializableFieldContainerHelper.ReadValueFromBytes(binaryReader, SerializableFieldContainerHelper.UnityObjectType, objs) as SpriteSheetInfoScript;
			sprite.spriteInfo = new SpriteInfo();
			sprite.spriteInfo.Read(binaryReader);
			return sprite;
    	}

    	return null;
    }
}